package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.FollowDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.service.ApUserFollowService;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.utils.thread.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 马毅爽
* @description 针对表【ap_user_follow(APP用户关注信息表)】的数据库操作Service实现
* @createDate 2023-12-19 16:59:28
*/
@Service
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow>
    implements ApUserFollowService{

    @Autowired
    private CacheService cacheService;

    @Override
    public ResponseResult follow(FollowDto dto) {
        // 1. 检查参数
        if (dto == null || dto.getArticleId() == null || dto.getAuthorId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 是否登录
        ApUser apUser = AppThreadLocalUtil.getUser();
        if (apUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 3. 保存用户行为
        String key = "FOLLOW-BEHAVIOR-" + dto.getArticleId().toString();
        String hashKey = apUser.getId().toString();
        if (dto.getOperation() == 0){
            Object object = cacheService.hGet(key, hashKey);
            if (object != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"禁止重复操作");
            }
            // 保存用户行为到redis
            cacheService.hPut(key, hashKey, JSON.toJSONString(dto));
            // 4. 保存关注数据到数据库
            ApUserFollow one = lambdaQuery()
                    .eq(ApUserFollow::getUserId, apUser.getId())
                    .eq(ApUserFollow::getFollowId, dto.getArticleId())
                    .one();
            if (one != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"禁止重复操作");
            }
            ApUserFollow apUserFollow = new ApUserFollow();
            apUserFollow.setFollowId(apUser.getId());
            apUserFollow.setFollowName(apUser.getName());
            apUserFollow.setUserId(dto.getAuthorId());
            apUserFollow.setLevel(0);
            apUserFollow.setIsNotice(0);
            apUserFollow.setCreatedTime(new Date());
            save(apUserFollow);
        }else {
            cacheService.hDelete(key, hashKey);
            remove(new LambdaQueryWrapper<ApUserFollow>()
                    .eq(ApUserFollow::getUserId, apUser.getId())
                    .eq(ApUserFollow::getFollowId, dto.getArticleId())
            );
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}




